<?php
  include("include/config.inc.php");
  $totalJama  = 0;
  $totalUdhar = 0;
  $displayJama = 0;
  $displayUdhar = 0;
  $totalRupiya = 0;
  $udhaarRupiya = 0;
  $totaljamaRupiya = 0;
  $totaludhaarRupiya = 0;
  $total = 0;
  $i=0;             
	$partyArray = array();
	
  $selectParty = "SELECT party.partyId,party.partyName,party.mobile1,city.city,remindDate
                    FROM party
                   LEFT JOIN city ON city.cityId = party.cityId 
                   WHERE 1 = 1";
  if(isset($_REQUEST['cityId']) && $_REQUEST['cityId'] != 0 )
  {
     $selectParty .= " AND party.cityId = ".$_REQUEST['cityId'];
  }
  if(isset($_REQUEST['reminders']) && $_REQUEST['reminders'] != 0 )
  {
  	 $selectParty .= " AND (requireRemind = 'Y'
  	                            AND remindDate <= '".date('Y-m-d')."')
                      ORDER BY remindDate DESC";
  }
  else
  {
  	$selectParty .= " ORDER BY city,partyName";
  }
  
  $selectPartyRes = mysql_query($selectParty);
  $i=0;
  while($partyRow = mysql_fetch_array($selectPartyRes))
  {
    $partyArray[$i]['jamaRupiya']   = 0; 
    $partyArray[$i]['udhaarRupiya'] = 0;
    $select="SELECT transaction.partyId,jamaRupiya,udhaarRupiya
  	           FROM transaction
  	          WHERE partyId=".$partyRow['partyId'];
    $res = mysql_query($select); 
    while($row = mysql_fetch_array($res)) 	
    {           
    	$partyArray[$i]['jamaRupiya']   += $row['jamaRupiya'];
    	$partyArray[$i]['udhaarRupiya'] += $row['udhaarRupiya'];
    }
  	$partyArray[$i]['partyId']       = $partyRow['partyId'];
  	$partyArray[$i]['partyName']     = $partyRow['partyName'];
  	$partyArray[$i]['mobile1']       = $partyRow['mobile1'];
  	$partyArray[$i]['city']          = $partyRow['city'];
  	$partyArray[$i]['remindDate']    = $partyRow['remindDate'];
  	$partyArray[$i]['total']         = $partyArray[$i]['jamaRupiya'] - $partyArray[$i]['udhaarRupiya'];
  	$totaljamaRupiya                += $partyArray[$i]['jamaRupiya'];
    $totaludhaarRupiya              += $partyArray[$i]['udhaarRupiya'];
    //	$totalJamaUdaharRupiya          = 
  	$i++;
  }
  $totalRupiya                     = $totaljamaRupiya - $totaludhaarRupiya;

//$displayJama  = $totalJama - $totalUdhar; 
//$displayUdhar = $totalUdhar - $totalJama; 
$smarty->assign('partyArray',$partyArray);
$smarty->assign('reminders',(isset($_REQUEST['reminders']) ? $_REQUEST['reminders'] : 0));
$smarty->assign('displayJama',$displayJama);
$smarty->assign('totalRupiya',$totalRupiya);
$smarty->assign('displayUdhar',$displayUdhar);
$smarty->assign('totalJama',$totalJama);
$smarty->assign('totalUdhar',$totalUdhar);
$smarty->assign('totaljamaRupiya',$totaljamaRupiya);
$smarty->assign('totaludhaarRupiya',$totaludhaarRupiya);
$smarty->assign('udhaarRupiya',$udhaarRupiya);
$smarty->assign('total',$total);
$smarty->display('summaryDataAj.tpl');
?>